package com.example.controller;

import com.example.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@RestController
@RequestMapping("/recognize")
@Slf4j
@CrossOrigin
public class RecognizeController {

    @PostMapping
    public Result resImage(){
        log.info("recognize运行了");
        Process proc;
        try {
//            String[] args = new String[]{"python", "E://vsProject/", String.valueOf(a), String.valueOf(b)};
            // 如果函数参数的话
            proc = Runtime.getRuntime().exec("python E://vsProject/test.py");
            //用输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while((line = in.readLine()) != null){
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return Result.success();
    }
}
